﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;

namespace Medianamik.UI.Web.UserControls
{
    public partial class LanguagePicker : BaseUserControl
    {
        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEditionCultures();
            }
        }

        protected void lvwEditionCultures_DataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                using (ListViewDataItem listViewDataItem = (ListViewDataItem)e.Item)
                {
                    if (listViewDataItem != null)
                    {
                        CultureInfo culture = (CultureInfo)listViewDataItem.DataItem;

                        //Activation du lien sur le nom du node
                        LinkButton link = (LinkButton)e.Item.FindControl("lnkCulture");

                        if (link != null)
                        {
                            link.CommandArgument = culture.Name;
                            link.Text = culture.TwoLetterISOLanguageName.ToUpper();
                            link.Enabled = (culture.Name != Page.CurrentEditionCulture.Name);

                            if (link.Enabled)
                            {
                                if (Page.CurrentPage.IsEditionPage)
                                {
                                    link.OnClientClick = String.Format("return confirm('{0}');", Page.SwitchLangMessage);
                                }
                            }
                            else
                            {
								link.CssClass = "Actif";
							}
                        }
                    }
                }
            }
        }

        protected void lvwEditionCultures_ItemCommand(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ChangeEditionCulture":
                    Page.CurrentEditionCulture = CultureInfo.GetCultureInfo(e.CommandArgument.ToString());
                    LoadEditionCultures();
                    break;
                default:
                    throw new Exception("Command " + e.CommandName + " is not supported for EditionCultures.");
            }
        }

        #endregion

        #region Methods

        private void LoadEditionCultures()
        {
            lvwEditionCultures.DataSource = Page.EditionCultures.Select(c=>c.CultureInfo);
            lvwEditionCultures.DataBind();
        }

        #endregion
    }
}